import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';

import 'my_separator_widget.dart';

class SelectSexWidget extends StatefulWidget {
  final Function(int sex)? selectSexOnTap;
  final int sex;
  final bool showBottomLine;
  final bool bottomIsSeparator;
  final double? titleWidth;
  final List<String> actions;
  final String? title;
  final double? spacing;

  const SelectSexWidget({
    Key ?key,
    this.selectSexOnTap,
    this.sex = 1,
    this.showBottomLine = true,
    this.bottomIsSeparator = true,
    this.titleWidth,
    this.actions = const ["男", "女"],
    this.title,
    this.spacing,
  }) : super(key: key);

  @override
  _SelectSexWidgetState createState() => _SelectSexWidgetState();
}

class _SelectSexWidgetState extends State<SelectSexWidget> {
  int ?sex;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.sex = widget.sex;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Container(
              width: widget.titleWidth ?? 80.px,
              height: 40.px,
              alignment: Alignment.centerLeft,
              child: Text(
                widget.title ?? "性别",
                style: TextStyle(
                  color: MyColors.formHex("#272727FF"),
                  fontSize: MyFont.font13,
                  fontWeight: MyFontWeight.regular,
                ),
              ),
            ),
          ]..addAll(List.generate(
              widget.actions.length,
              (index) => buildItem(index),
            )),
        ),
        Offstage(
          offstage: !widget.showBottomLine,
          child: Row(children: [
            SizedBox(width: widget.titleWidth ?? 80.px),
            Expanded(
              child: widget.bottomIsSeparator
                  ? MySeparator(color: MyColors.color_238_238_238)
                  : Divider(
                      height: 1,
                      color: MyColors.color_238_238_238,
                    ),
            ),
          ]),
        )
      ],
    );
  }

  Widget buildItem(int index) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (widget.selectSexOnTap != null) widget.selectSexOnTap?.call(sex!);
        sex = index;
        print(index);
        setState(() {});
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          MyAssetImage(
            sex == index ? "icon_radio" : "icon_un_radio",
            width: 13.px,
            height: 13.px,
          ),
          MySizeBox(width: 3.px),
          Text(
            widget.actions[index],
            style: TextStyle(
              color: MyColors.formHex("#272727FF"),
              fontSize: MyFont.font10,
              fontWeight: MyFontWeight.regular,
            ),
          ),
          MySizeBox(
            width: index != widget.actions.length - 1 ? (widget.spacing ?? 20.px) : 0,
          ),
        ],
      ),
    );
  }
}
